#ifndef FAMILY_H
#define FAMILY_H

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <fstream>
#include <sstream>

// 家庭类
class FamilyMember
{
public:
    std::string name;
    std::string birthDate;
    std::string contactInfo;
    std::vector<FamilyMember *> children;

    bool isMarried;         // 婚否
    bool isAlive;           // 健在否
    std::string deathDate;  // 死亡日期
    std::string fatherName; // 父亲姓名
    std::string motherName; // 母亲姓名

    FamilyMember(std::string name, std::string birthDate, std::string contactInfo, bool isMarried = false, bool isAlive = true, std::string deathDate = "", std::string fatherName = "", std::string motherName = "")
        : name(name), birthDate(birthDate), contactInfo(contactInfo), isMarried(isMarried), isAlive(isAlive), deathDate(deathDate), fatherName(fatherName), motherName(motherName) {}

    ~FamilyMember()
    {
        for (auto child : children)
        {
            delete child;
        }
    }
};

// 家庭树
class FamilyTree
{
private:
    FamilyMember *root; // 树的根节点
    std::map<std::string, FamilyMember *> memberMap;

public:
    FamilyTree() : root(nullptr) {}

    ~FamilyTree()
    {
        delete root;
    }

    FamilyMember *getRoot() const { return root; }

    bool findMemberPath(FamilyMember *node, const std::string &name, std::vector<FamilyMember *> &path)
    {
        if (node == nullptr)
            return false;
        path.push_back(node);
        if (node->name == name)
            return true;
        for (auto child : node->children)
        {
            if (findMemberPath(child, name, path))
                return true;
        }
        path.pop_back();
        return false;
    }

    void addMember(FamilyMember *parent, FamilyMember *child)
    {
        if (parent == nullptr)
        {
            // 如果parent为空，则将child设为根节点
            root = child;
        }
        else
        {
            // 否则，将child添加到parent的子列表中
            parent->children.push_back(child);
        }
        // 将child添加到成员映射中，以便后续查找
        memberMap[child->name] = child;
    }
    bool searchByBirthDateHelper(FamilyMember *node, const std::string &birthDate);
    FamilyMember *findMemberByName(const std::string &name);
    // void addMember(FamilyMember* parent, FamilyMember* child);
    void deleteMember(FamilyMember *member);
    void modifyMember(FamilyMember *member, const std::string &newName, const std::string &newBirthDate, const std::string &newContactInfo, const std::string &newIsMarried, const std::string &newIsAlive, const std::string &newDeathDate, const std::string &newFatherName, const std::string &newMotherName);
    void displayTree();
    void levelOrderTraversal();
    void displayNthGeneration(int n);
    // void searchByName(std::string name);
    void searchByBirthDate(std::string birthDate);
    void determineRelationship(const std::string &name1, const std::string &name2);
    void sortMembersByBirthDate();
    void sortMembersByAge();
    void preOrderTraversal(FamilyMember *node);
    void postOrderTraversal(FamilyMember *node);
    void inOrderTraversal(FamilyMember *node);
    // void buildBinarySearchTree();
    void readCSV(std::string filename);
    int calculateHeight(FamilyMember *node);
    bool isBalanced(FamilyMember *node);
    void addChild(FamilyMember *father, FamilyMember *mother, FamilyMember *child);
};

#endif // FAMILY_H